package com.crack.nga2.localimage;

import android.graphics.Bitmap;
import android.graphics.Point;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.crack.nga2.R;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class SelectImageActivity extends AppCompatActivity {
    private GridView gridView;
    private List<String> imagePath;
    private NativeImageLoader imageLoader;
    private List<String> selected = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_image);
        gridView = (GridView) findViewById(R.id.gridView);
        imageLoader = NativeImageLoader.getInstance();
        imagePath = getIntent().getStringArrayListExtra("images");
        gridView.setAdapter(new ImageAdapter());
    }

    public void doClick(View view) {
        switch (view.getId()) {
            case R.id.send:
                break;
            case R.id.select_image:
                int position = (int) view.getTag();
                String path = imagePath.get(position);

                ImageView albumView = (ImageView) gridView.findViewWithTag(path);
                albumView.setDrawingCacheEnabled(true);
                Bitmap bitmap = albumView.getDrawingCache();
                albumView.setDrawingCacheEnabled(false);

                ImageView imageView = (ImageView) view;
                if (selected.contains(path)) {
                    selected.remove(path);
                    EventBus.getDefault().post(new SelectEvent(bitmap, path, false));
                    imageView.setImageResource(R.drawable.selected);
                } else {
                    selected.add(path);
                    EventBus.getDefault().post(new SelectEvent(bitmap, path, true));
                    imageView.setImageResource(R.drawable.image_unselected_icon);
                }
                break;
        }
    }

    private class ImageAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return imagePath.size();
        }

        @Override
        public Object getItem(int position) {
            return imagePath.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View view, final ViewGroup parent) {
            final ImageView imageView;
            ImageView selectImage;
            if (view == null) {
                view = View.inflate(SelectImageActivity.this, R.layout.post_select_image_item, null);
                imageView = (ImageView) view.findViewById(R.id.select_image);
                view.setTag(imageView);
            } else {
                imageView = (ImageView) view.getTag();
            }
            String path = imagePath.get(position);
            selectImage = (ImageView) view.findViewById(R.id.select_state);
            selectImage.setTag(position);
            imageView.setTag(path);
            imageLoader.loadNativeImage(path, new Point(95, 95), new NativeImageLoader.NativeImageCallBack() {
                @Override
                public void onImageLoader(Bitmap bitmap, String path) {
                    if (imageView.getTag() == path) {
                        imageView.setImageBitmap(bitmap);
                    }
                }
            });
            return view;
        }
    }

}
